package com.youdo.careyes.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.youdo.careyes.R;
import com.youdo.careyes.utils.ToastUtil;

/**
 * 自定义悬浮框
 *
 * @author ljb
 * @date 2025.07.25
 * @email 568838953@qq.com
 * <p>
 */
public class BxFloatButton extends com.google.android.material.floatingactionbutton.FloatingActionButton {
    private int pressedColor = Color.RED; // 点击时的颜色
    private int normalColor = Color.WHITE;  // 正常状态的颜色
    private Drawable originalIcon; // 原始图标
    private static final long DEFAULT_DEBOUNCE_INTERVAL = 2000; // 默认2秒
    private long lastClickTime = 0;
    private OnClickListener debouncedClickListener;
    private Runnable pendingClickRunnable;

    // ====================================
    // ===========构造方法-初始化===========
    // ====================================

    /**
     * 在java代码里new的时候会用到
     */
    public BxFloatButton(Context context) {
        super(context);
        initView();
    }

    /**
     * 在xml布局文件中使用时自动调用
     */
    public BxFloatButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    /**
     * 不会自动调用，如果有默认style时，在第二个构造函数中调用
     */
    public BxFloatButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    /**
     * 初始化UI，可根据业务需求设置默认值。
     */
    private void initView() {
        // 默认颜色
        pressedColor = getResources().getColor(R.color.color_vector_press);
        normalColor = getResources().getColor(R.color.color_vector_normal);
        // 保存原始图标
        originalIcon = getDrawable();

        super.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long currentTime = System.currentTimeMillis();
                if (currentTime - lastClickTime > DEFAULT_DEBOUNCE_INTERVAL) {
                    Log.i("BxFloatButton", "执行点击事件:" + getContentDescription());
                    lastClickTime = currentTime;
                    if (debouncedClickListener != null) {
                        debouncedClickListener.onClick(v);
                    }
                } else {
                    Log.i("BxFloatButton", "忽略点击事件:" + getContentDescription());
                    ToastUtil.showShort(R.string.app_public_too_fast);
                }
            }
        });
    }

    /**
     * 点击事件
     */
    @Override
    public void setOnClickListener(OnClickListener l) {
        this.debouncedClickListener = l;
    }

    /**
     * 当视图的焦点状态发生变化时
     */
    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        setBackgroundTintList(ContextCompat.getColorStateList(getContext(), gainFocus ? R.color.floating_button_bg_focused_color : R.color.floating_button_bg_color));
    }

    /**
     * 触摸事件
     */
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 按下时改变图标颜色
                changeIconColor(pressedColor);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // 抬起或取消时恢复原始颜色
                changeIconColor(normalColor);
                break;
        }
        return super.onTouchEvent(ev);
    }

    private void changeIconColor(int color) {
        if (originalIcon != null) {
            Drawable wrappedDrawable = originalIcon.getConstantState().newDrawable().mutate();
            wrappedDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            setImageDrawable(wrappedDrawable);
        }
    }
}